﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Trio.Motor;
using Atc600Control;

namespace RollController2022
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        System.Timers.Timer getvalue_timer = new System.Timers.Timer();//位置速度取值定时器
        //System.Timers.Timer Limit_timer = new System.Timers.Timer();

        int move_axis = 10;//轴10为左电机；
        int back_axis = 3; //轴3为右电机；
        int Encoder_axis = 0; //编码器轴0；
        int Tension_axis = 9; //左磁粉DA9；
        int UD = 7; //步进上下轴7；
        int FB = 8; //步进前后轴8；
        TrioP600 trio = new TrioP600();
        Atc600 atc600 = new Atc600();
        private void Form3_Load(object sender, EventArgs e)
        {
            SetMode();//模式选择显示隐藏输入框
            trio.Connect();//通讯连接
            trio.RollInitMpos(Encoder_axis);//编码器清零
            atc600.Connect();//通讯连接
            trio.SetTension(Tension_axis, 1);//左磁粉初始
            atc600.SetTension(0, 1);//右磁粉手动值1N
            getvalue_timer.Interval = 50;
            getvalue_timer.Elapsed += new ElapsedEventHandler(value_Elapsed);
            //Limit_timer.Interval = 50;
            //Limit_timer.Elapsed += new ElapsedEventHandler(limit_Elapsed);
            //Limit_timer.Start();
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Stop();//停止显示
            getvalue_timer.Stop();//停止读取
            trio.Closed();//停止运动，断开连接
            atc600.Closed();//断开连接
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            trio.RollMove(FB, 10, -500);
            timer2.Start();
        }

        int numtimer2 = 0;
        double lastEc, Ec;
        private void timer2_Tick(object sender, EventArgs e)
        {
            numtimer2++;
            Ec = Math.Abs(trio.GetEncoder(Encoder_axis));
            if (numtimer2 > 10)
            {
                if (Ec == lastEc)
                {
                    Ec = 0;
                    lastEc = 0;
                    numtimer2 = 0;
                    btn_Up.Enabled = true;
                    btn_Dowm.Enabled = true;
                    btn_Face.Enabled = true;
                    btn_Back.Enabled = true;

                    lb_LTorque.Enabled = true;
                    numUpD_LTorque.Enabled = true;
                    lb_LAngle.Enabled = true;
                    numUpD_LAngle.Enabled = true;

                    lb_RAngle.Enabled = true;
                    numUpD_RAngle.Enabled = true;
                    lb_RTorque.Enabled = true;
                    numUpD_RTorque.Enabled = true;

                    cb_LReverse.Enabled = true;
                    cb_RReverse.Enabled = true;

                    rbtn_LayerWinding.Enabled = true;
                    rbtn_Back.Enabled = true;

                    cb_Length.Enabled = true;
                    tbx_Length.Enabled = true;
                    cb_Turns.Enabled = true;
                    tbx_Turns.Enabled = true;
                    cb_Layer.Enabled = true;
                    tbx_Layer.Enabled = true;

                    btn_L.Enabled = true;
                    btn_R.Enabled = true;

                    layer_read = 0;

                    lb_Length.Text = "0.00";//长度
                    lb_Turns.Text = "0.0";//圈数
                    lb_Layer.Text = "0";//层数
                    lb_Long.Text = "0.0";//层绕长度
                    btn_Start.Text = "Start";

                    trio.RollInitMpos(FB);
                    timer2.Stop();
                }
                else
                {
                    lastEc = Ec;
                }
            }
        }

        private void cb_Length_Click(object sender, EventArgs e)
        {
            if (cb_Length.Checked == true)
            {
                cb_Turns.Checked = false;
                cb_Layer.Checked = false;
            }
        }

        private void cb_Turns_Click(object sender, EventArgs e)
        {
            if (cb_Turns.Checked == true)
            {
                cb_Length.Checked = false;
                cb_Layer.Checked = false;
            }
        }

        private void cb_Layer_Click(object sender, EventArgs e)
        {
            if (cb_Layer.Checked == true)
            {
                cb_Length.Checked = false;
                cb_Turns.Checked = false;
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (btn_Start.Text == "Start")
            {
                btn_Up.Enabled = false;
                btn_Dowm.Enabled = false;
                btn_Face.Enabled = false;
                btn_Back.Enabled = false;

                lb_LTorque.Enabled = false;
                numUpD_LTorque.Enabled = false;
                lb_LAngle.Enabled = false;
                numUpD_LAngle.Enabled = false;

                lb_RAngle.Enabled = false;
                numUpD_RAngle.Enabled = false;
                lb_RTorque.Enabled = false;
                numUpD_RTorque.Enabled = false;

                cb_LReverse.Enabled = false;
                cb_RReverse.Enabled = false;

                rbtn_LayerWinding.Enabled = false;
                rbtn_Back.Enabled = false;

                cb_Length.Enabled = false;
                tbx_Length.Enabled = false;
                cb_Turns.Enabled = false;
                tbx_Turns.Enabled = false;
                cb_Layer.Enabled = false;
                tbx_Layer.Enabled = false;

                btn_L.Enabled = false;
                btn_R.Enabled = false;
                btn_Clear.Enabled = false;

                trio.RollInitMpos(Encoder_axis);

                SetMove();
                getvalue_timer.Start();//打开定时器
                timer1.Start();

                btn_Start.Text = "Stop";
            }
            else if (btn_Start.Text == "Continue")
            {
                btn_Clear.Enabled = false;

                cb_Length.Enabled = false;
                tbx_Length.Enabled = false;
                cb_Turns.Enabled = false;
                tbx_Turns.Enabled = false;
                cb_Layer.Enabled = false;
                tbx_Layer.Enabled = false;

                SetMove();

                if (dirt == 1)
                {
                    trio.RollMove(FB, returnspeed, Long_set);//走线轴8与方向前后
                }
                else if (dirt == 0)
                {
                    trio.RollMove(FB, returnspeed, Long_set * (-1));//走线轴8与方向前后
                }

                getvalue_timer.Start();//打开定时器
                timer1.Start();

                btn_Start.Text = "Stop";
            }
            else if (btn_Start.Text == "Stop")
            {
                btn_Clear.Enabled = true;

                cb_Length.Enabled = true;
                tbx_Length.Enabled = true;
                cb_Turns.Enabled = true;
                tbx_Turns.Enabled = true;
                cb_Layer.Enabled = true;
                tbx_Layer.Enabled = true;

                timer1.Stop();//停止显示
                getvalue_timer.Stop();//停止读取
                trio.StopMove();//停止运动
                trio.SetTension(Tension_axis, 1);//左磁粉初始
                atc600.SetTension(0, 1);//右磁粉手动值1N
                btn_Start.Text = "Continue";
            }
        }

        double Long_set, returnspeed, Langle_set, Rangle_set;
        double length_read, turns_read, layer_read = -1, long_read;
        int dirt = 0;
        DateTime dt_last;
        TimeSpan Time;
        double Encoder, Encoder_last;
        private void value_Elapsed(object sender, ElapsedEventArgs e)
        {
            Encoder_last = Encoder;
            Encoder = Math.Abs(trio.GetEncoder(Encoder_axis));
            length_read = Encoder / 8000.0 * 0.16 * 3.1415926;//m
            turns_read = Encoder / 8000;

            long_read = Math.Abs(trio.GetEncoder(FB));//mm

            DateTime dt_now = DateTime.Now;
            Time = dt_now.Subtract(dt_last);

            //speed_read = Math.Abs(Encoder_last - Encoder) / 8000.0 * 0.16 * 3.1415926 / Time.TotalMilliseconds * 1000;
            dt_last = dt_now;

            if (long_read >= (Long_set - 0.001))
            {
                trio.RollMove(FB, returnspeed, Long_set);//走线轴8与方向前后
                layer_read++;
                dirt = 1;
            }
            else if (long_read <= 0.001)
            {
                trio.RollMove(FB, returnspeed, Long_set * (-1));//走线轴8与方向前后
                layer_read++;
                dirt = 0;
            }

            if (cb_Length.Checked == true)//长度限制
            {
                CheckLimit();
            }
            else if (cb_Turns.Checked == true)//圈数限制
            {
                CheckLimit();
            }
            else if (cb_Layer.Checked == true)//层数限制
            {
                CheckLimit();
            }
        }

        private void CheckLimit()
        {
            if (cb_Length.Checked == true)//长度限制
            {
                if (length_read >= Convert.ToDouble(tbx_Length.Text))
                {
                    btn_Start.PerformClick();
                    MessageBox.Show("");
                }
            }
            else if (cb_Turns.Checked == true)//圈数限制
            {
                if (turns_read >= Math.Round(Convert.ToDouble(tbx_Turns.Text), 1))
                {
                    btn_Start.PerformClick();
                    MessageBox.Show("");
                }
            }
            else if (cb_Layer.Checked == true)//层数限制
            {
                if (layer_read >= Math.Round(Convert.ToDouble(tbx_Layer.Text), 0))
                {
                    btn_Start.PerformClick();
                    MessageBox.Show("");
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lb_Length.Text = Math.Round(length_read, 2).ToString();
            lb_Turns.Text = Math.Round(turns_read, 1).ToString();
            lb_Layer.Text = layer_read.ToString();
            lb_Long.Text = Math.Round(long_read, 1).ToString();
        }

        //手动上下
        private void btn_Up_MouseDown(object sender, MouseEventArgs e)
        {
            trio.RollMove(UD, 10, 500);
        }
        private void btn_Up_MouseUp(object sender, MouseEventArgs e)
        {
            trio.StopMove();
        }
        private void btn_Dowm_MouseDown(object sender, MouseEventArgs e)
        {
            trio.RollMove(UD, 10, -500);
        }
        private void btn_Dowm_MouseUp(object sender, MouseEventArgs e)
        {
            trio.StopMove();
        }
        //手动前后
        private void btn_Face_MouseDown(object sender, MouseEventArgs e)
        {
            trio.RollMove(FB, 10, 500);
        }
        private void btn_Face_MouseUp(object sender, MouseEventArgs e)
        {
            trio.StopMove();
            System.Threading.Thread.Sleep(100);
            numUpD_Long.Maximum = Math.Round(20 - (decimal)trio.GetEncoder(FB), 1);
        }
        private void btn_Back_MouseDown(object sender, MouseEventArgs e)
        {
            trio.RollMove(FB, 10, -500);
        }
        private void btn_Back_MouseUp(object sender, MouseEventArgs e)
        {
            trio.StopMove();
            System.Threading.Thread.Sleep(100);
            numUpD_Long.Maximum = Math.Round(20 - (decimal)trio.GetEncoder(FB), 1);
        }
        //手动左右
        private void btn_L_MouseDown(object sender, MouseEventArgs e)
        {
            int L_dir = 0;
            if (cb_LReverse.Checked == true) L_dir = 1;
            else if (cb_LReverse.Checked == false) L_dir = 0;

            trio.RoollInitMotor(move_axis, L_dir);//走线轴10与方向
            trio.SetTension(Tension_axis, 50);//左张力50N
            atc600.SetTension(1, 1);//右张力
            trio.CtrlSpeed(move_axis, 500);//
        }

        private void btn_L_MouseUp(object sender, MouseEventArgs e)
        {
            trio.StopMove();
        }
        private void btn_R_MouseDown(object sender, MouseEventArgs e)
        {
            int R_dir = 0;
            if (cb_RReverse.Checked == true) R_dir = 1;
            else if (cb_RReverse.Checked == false) R_dir = 0;

            trio.RoollInitMotor(back_axis, R_dir);//右轴3与方向
            trio.SetTension(Tension_axis, 1);//左张力设置
            atc600.SetTension(0, 50);//右张力50N
            trio.CtrlSpeed(back_axis, 500);//rpm
        }
        private void btn_R_MouseUp(object sender, MouseEventArgs e)
        {
            trio.StopMove();
        }

        private void rbtn_LayerWinding_CheckedChanged(object sender, EventArgs e)
        {
            SetMode();
        }
        private void rbtn_Back_CheckedChanged(object sender, EventArgs e)
        {
            SetMode();
        }

        private void SetMove()
        {
            Long_set = Math.Round((double)numUpD_Long.Value, 1);
            Langle_set = Math.Round((double)numUpD_LAngle.Value, 1);//rps
            Rangle_set = Math.Round((double)numUpD_RAngle.Value, 1);
            returnspeed = Langle_set * Math.Round((double)numUpD_Diameter.Value, 1);//mm/s

            int L_dir = 0, R_dir = 0;
            if (cb_LReverse.Checked == true) L_dir = 1;
            else if (cb_LReverse.Checked == false) L_dir = 0;

            if (cb_RReverse.Checked == true) R_dir = 1;
            else if (cb_RReverse.Checked == false) R_dir = 0;

            if (rbtn_LayerWinding.Checked == true)
            {
                trio.RoollInitMotor(move_axis, L_dir);//走线轴10与方向

                trio.SetTension(Tension_axis, 50);//左张力50N
                atc600.SetTension(1, (int)numUpD_RTorque.Value);//右张力设置

                trio.CtrlSpeed(move_axis, Langle_set * 8000);//恒角走线 转/s
            }
            else if (rbtn_Back.Checked == true)
            {
                trio.RoollInitMotor(back_axis, R_dir);//右轴3与方向

                trio.SetTension(Tension_axis, (double)numUpD_LTorque.Value);//左张力设置
                atc600.SetTension(0, 50);//右张力50N

                trio.CtrlSpeed(back_axis, Rangle_set * 8000);//rps
            }
        }

        private void SetMode()
        {
            if (rbtn_LayerWinding.Checked == true)//层绕，恒角
            {
                numUpD_LAngle.Visible = true;//左角速度
                lb_LAngle.Visible = true;
                numUpD_RTorque.Visible = true;//右张力
                lb_RTorque.Visible = true;

                numUpD_LTorque.Visible = false;//左张力
                lb_LTorque.Visible = false;
                numUpD_LAngle.Visible = false;//右角速度
                lb_LAngle.Visible = false;
            }
            else if (rbtn_Back.Checked == true)//退线，恒角
            {
                numUpD_LAngle.Visible = false;//左角速度
                lb_LAngle.Visible = false;
                numUpD_RTorque.Visible = false;//右张力
                lb_RTorque.Visible = false;

                numUpD_LTorque.Visible = true;//左张力
                lb_LTorque.Visible = true;
                numUpD_LAngle.Visible = true;//右角速度
                lb_LAngle.Visible = true;
            }
        }
    }
}
